#coding=utf-8
import os.path

import numpy as np
import  util
import  matplotlib.pyplot as plt
import pylab
import cv2
import cv2 as cv
import timeit
import sys
import time
from typing import Tuple
from util import  global_variable
import watershed

start = time.process_time()
workspace_base_dir  = "/home/hhd/workspace/longi_coal_picture/"
jing_kuang_dir = workspace_base_dir + "jingKuang"
wei_kuang_dir = workspace_base_dir + "weiKuang"
zhong_kuang_1 = workspace_base_dir + "zhongKuang1"
zhong_kuang_2 = workspace_base_dir + "zhongKuang2"
hardToDivide = workspace_base_dir +  "hardToDivide"

all_coal_dir = [jing_kuang_dir,zhong_kuang_1,zhong_kuang_2,wei_kuang_dir]




# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/2253_00222_25_004.tif"
# img_name = "/home/hhd/workspace/longi_coal_picture/jingKuang/505_00069_30_003.tif"
# img_name = hardToDivide + "/" + "1002_00014_17_001.tif" #背景颜色较j hard1
# img_name = hardToDivide + "/" + "1625_00914_30_009.tif" #背景颜色较深
# img_name = hardToDivide + "/" + "2286_00231_20_024.tif" #  煤块有重叠
# img_name = hardToDivide + "/" + "2346_00250_20_010.tif" #  部分点颜色太
# img_name = hardToDivide + "/" + "418_00090_30_001.tif" #  部分点颜色太淡
# img_name = hardToDivide + "/" + "766_00153_24_003.tif" #  部分点颜色太淡
# img_name = jing_kuang_dir + "/" + "505_00069_30_003.tif"
# img_name = jing_kuang_dir + "/" + "509_00143_24_006.tif" # 509图像边界有问题，分割出来的左侧图像有纯黑色阴影 828也有同样的问题
# img_name = zhong_kuang_1 + "/" + "1582_00151_32_001.tif" #去除1582的较小的点
# img_name = zhong_kuang_2 + "/" + "2620_01710_31_006.tif" #去除1582的较小的点
# img_name = wei_kuang_dir + "/" + "2253_00222_25_004.tif"
# img_name = wei_kuang_dir + "/" + "2275_01639_19_014.tif" #小点250
# img_name = wei_kuang_dir + "/" + "2289_00232_26_019.tif" # 图像扩张后粘连
# img_name = wei_kuang_dir + "/" + "2287_00054_21_019.tif" #图像粘连
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/2289_00232_26_019.tif"
# 典型粘连:
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/2350_00063_18_014.tif"  # 基于距离变换的分水岭分割不好情况
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/2286_00231_20_024.tif" 
# 异常图像
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/2383_00260_18_002.tif"
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/2345_01666_19_015.tif"
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/2823_00292_21_017.tif" # 基于距离变换的分水岭分割不好情况 -- 过分割
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/2834_00297_19_005.tif" # 过小了检测长度敏感
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/2322_00241_21_035.tif" #论文里主要的分割图像
img_name = "./img/2322_00241_21_035.tif"
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/2835_01993_18_014.tif" # 三个图像相互粘连图像
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/2280_00229_19_011.tif"
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/5675_00769_24_001.tif" # 大长石块
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/2338_00061_20_020.tif"
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/2277_01640_18_008.tif"
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/2301_00056_30_029.tif" # 粘连角度过大 距离变换分水岭分不开
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/2358_01671_22_031.tif" #基于距离变换的分水岭分割不好情况 -- 内部过分割
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/2820_00291_18_016.tif" #基于距离变换的分水岭分割不好情况 -- 粘连面积过大,无法分割开 -- 非常典型 zhan1
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/2291_00233_24_027.tif" #基于距离变换的分水岭分割不好情况 --  对于颜色比较浅,会出现分割效果很不理想的情况
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/2317_01655_21_029.tif" #基于距离变换的分水岭分割不好情况 --  内部出现过分割
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/2316_00239_26_027.tif" #基于距离变换的分水岭分割不好情况 --  能分割,但是分割线有问题
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/5727_00379_27_001.tif" #简单凹点匹配方法分割错误 --  没有调整半径导致过分割
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/2307_01652_33_032.tif" #简单凹点匹配方法分割错误 --  同一侧错误
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/2315_01654_19_028.tif" #简单凹点匹配方法分割错误 --  过分割
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/5134_00707_18_005.tif" #简单凹点匹配方法分割错误 --  晶体特性图像
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/2317_01655_21_029.tif" #简单凹点匹配方法分割错误 --  过分割
# img_name = "/home/hhd/workspace/longi_coal_picture/weiKuang/2308_00057_18_020.tif" #简单凹点匹配方法分割错误 --  假的相互粘连 三个图像
# img_name = wei_kuang_dir + "/" + "2287_00054_21_019.tif" # 假的三个图像相互粘连

# zhongkuagn2 图像2620处理，大小圈嵌套


#1002_00014_17_001.tif  1625_00914_30_009.tif  2286_00231_20_024.tif  2346_00250_20_010.tif  418_00090_30_001.tif  766_00153_24_003.tif

global_variable = {}
 #/home/hhd/workspace/longi_coal_picture/weiKuang/2280_00229_19_011.tif


print("命令行参数: " + sys.argv[1])


if(sys.argv[1] == "simple"):
    util.is_use_simple = True
if(sys.argv[1] == "watershed"):
    util.is_use_watershed = True




def get_segmetation_result(img_name):
    print("===================新的图像========================================")
    print(img_name)
    img_left_u8 = util.getU8LeftImageFromTif(img_name)
    if util.is_use_watershed:
        watershed.watershed_with_distance_transfrom(img_left_u8)
    else:
        segmentation_contours_tuple = util.get_segmentation_result_from_img_left_u8_c1(img_left_u8)


    # print("shape: " ,img_left_u8.shape)
    # util.cv2_imwrite("img_left_u8", img_left_u8)
    # segmentation_contours_tuple = util.distance_transform_based_watershed(img_left_u8)


    #绘制彩色图像
    # black_background = util.get_mask_from_contour(img_left_u8,segmentation_contours_tuple, -1,is_use_black_background=False,put_index=True)
    # util.imshow2("origin_img",img_left_u8,"segmentation_result",black_background)

# get_segmetation_result("/tmp/img_left_u8.png")



# util.exec_fun_for_files_in_dir(jing_kuang_dir,testThresholdAlgorithmForSingleImgWrapper)
# util.exec_fun_for_files_in_dir(zhong_kuang_1,testThresholdAlgorithmForSingleImgWrapper)
# util.exec_fun_for_files_in_dir(zhong_kuang_2,testThresholdAlgorithmForSingleImgWrapper)

get_segmetation_result(img_name)
# util.exec_fun_for_files_in_dir(wei_kuang_dir,get_segmetation_result)
# util.exec_fun_for_files_in_dir(wei_kuang_dir,util.print_file_name)

# print(global_variable)




end = time.process_time()

print("程序运行时间: " ,end - start ," seconds" )
